package com.sugar.psych.assessment.module.business.controller.admin.avatarinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.CommonResult;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;
import static com.sugar.psych.assessment.framework.common.pojo.CommonResult.success;

import com.sugar.psych.assessment.framework.excel.core.util.ExcelUtils;

import com.sugar.psych.assessment.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.psych.assessment.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.psych.assessment.module.business.controller.admin.avatarinfo.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.avatarinfo.AvatarInfoDO;
import com.sugar.psych.assessment.module.business.service.avatarinfo.AvatarInfoService;

@Tag(name = "管理后台 - 随机头像")
@RestController
@RequestMapping("/business/avatar-info")
@Validated
public class AvatarInfoController {

    @Resource
    private AvatarInfoService avatarInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建随机头像")
    @PreAuthorize("@ss.hasPermission('business:avatar-info:create')")
    public CommonResult<Long> createAvatarInfo(@Valid @RequestBody AvatarInfoSaveReqVO createReqVO) {
        return success(avatarInfoService.createAvatarInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新随机头像")
    @PreAuthorize("@ss.hasPermission('business:avatar-info:update')")
    public CommonResult<Boolean> updateAvatarInfo(@Valid @RequestBody AvatarInfoSaveReqVO updateReqVO) {
        avatarInfoService.updateAvatarInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除随机头像")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:avatar-info:delete')")
    public CommonResult<Boolean> deleteAvatarInfo(@RequestParam("id") Long id) {
        avatarInfoService.deleteAvatarInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除随机头像")
                @PreAuthorize("@ss.hasPermission('business:avatar-info:delete')")
    public CommonResult<Boolean> deleteAvatarInfoList(@RequestParam("ids") List<Long> ids) {
        avatarInfoService.deleteAvatarInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得随机头像")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:avatar-info:query')")
    public CommonResult<AvatarInfoRespVO> getAvatarInfo(@RequestParam("id") Long id) {
        AvatarInfoDO avatarInfo = avatarInfoService.getAvatarInfo(id);
        return success(BeanUtils.toBean(avatarInfo, AvatarInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得随机头像分页")
    @PreAuthorize("@ss.hasPermission('business:avatar-info:query')")
    public CommonResult<PageResult<AvatarInfoRespVO>> getAvatarInfoPage(@Valid AvatarInfoPageReqVO pageReqVO) {
        PageResult<AvatarInfoDO> pageResult = avatarInfoService.getAvatarInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AvatarInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出随机头像 Excel")
    @PreAuthorize("@ss.hasPermission('business:avatar-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAvatarInfoExcel(@Valid AvatarInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AvatarInfoDO> list = avatarInfoService.getAvatarInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "随机头像.xls", "数据", AvatarInfoRespVO.class,
                        BeanUtils.toBean(list, AvatarInfoRespVO.class));
    }

}